package cn.shengongying.api.dto;

import cn.shengongying.bean.BaseUser;
import cn.shengongying.enums.MsgEnum;
import cn.shengongying.model.BossPosition;
import cn.shengongying.model.Information;
import cn.shengongying.model.UserPositionApplyRecord;
import cn.shengongying.utils.DateUtils;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * author: RXK
 * date: 2022/1/10 星期一
 * desc:
 */
@Getter
@Setter
public class PositionInviteInterviewInDTO extends IdInDTO {

    @ApiModelProperty(value = "邀请类型(0:附近邀请;1:简历筛选邀请)")
    @NotNull(message = "类型不能为空")
    private Integer type;

    @ApiModelProperty(value = "邀请的用户id")
    @NotEmpty(message = "至少邀请一位用户")
    private List<Long> userId;

    @ApiModelProperty(value = "面试日期(yyyy-MM-dd))", required = false)
    @JsonFormat(pattern = "yyyy-MM-dd")
    private Date resumeDate;


    public List<UserPositionApplyRecord> buildApplyRecord(List<UserPositionApplyRecord> recordList) {
        return recordList.stream()
                .map(old -> {
                    UserPositionApplyRecord update = new UserPositionApplyRecord();
                    update.setId(old.getId());
                    update.setInterviewTime(this.resumeDate);
                    update.setStatus(1);
                    return update;
                })
                .collect(Collectors.toList());
    }

    public List<Information> buildMsg(BaseUser baseUser, BossPosition position) {
        return this.userId.stream()
                .map(user -> {
                    Information information = getInformation(baseUser, position, user);
                    information.setContent(String.format(MsgEnum.invite_interview_msg.getMsg(), position.getCompanyName(), baseUser.getNickName(), position.getPositionName()));
                    return information;
                })
                .collect(Collectors.toList());
    }



    public List<Information> buildInterviewMsg(BaseUser baseUser, BossPosition position) {
        return this.userId.stream()
                .map(user -> {
                    Information information = getInformation(baseUser, position, user);
                    information.setContent(String.format(MsgEnum.invite_interview.getMsg(), position.getCompanyName(), baseUser.getNickName(), DateUtils.date2Str(this.getResumeDate(), DateUtils.date_time_minus), position.getPositionName()));
                    return information;
                })
                .collect(Collectors.toList());
    }

    private Information getInformation(BaseUser baseUser, BossPosition position, Long user) {
        Information information = new Information();
        information.setUserId(user);
        information.setSourceId(position.getId());
        information.setFrom(baseUser.getId());
        information.setType(4);
        information.setRead(0);
        return information;
    }

    //去重以后的用户id
    public Set<Long> userId() {
        return new HashSet<>(this.userId);
    }
}
